package org.misty.practices.string;

import java.lang.reflect.Field;
import java.util.Arrays;

public class StringTestB {
    static final String JAVA = "Java";
    static final Field valueField;

    static {
        Field field;
        try {
            field = String.class.getDeclaredField("value");
            field.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            field = null;
        }
        valueField = field;
    }

    public static void main(String[] args) throws IllegalAccessException {
        testB();
    }

    public static void testB() throws IllegalAccessException {
        var prefix = "Hello";

        var s1 = "HelloWorld";
        var s2 = new String(prefix + "World");

        byte[] bytes1 = (byte[]) valueField.get(s1);
        byte[] bytes2 = (byte[]) valueField.get(s2);
        System.out.println(s1 == s2); // false
        System.out.println(bytes1); // [B@ba4d54
        System.out.println(bytes2); // [B@12bc6874
        System.out.println(Arrays.equals(bytes1, bytes2)); // true

        System.out.println(valueField.get(s2.intern())); // [B@ba4d54
    }
}
